package com.wedo.search.listener;

import com.wedo.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class BookListener {

    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEDO.SEARCH.SAVE.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEDO.BOOK.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"book.insert", "book.update"}
    ))
    public void save(Long id) throws IOException {
        if (id == null) {
            return;
        }
        this.searchService.save(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "WEDO.SEARCH.DELETE.QUEUE",durable = "true"),
            exchange = @Exchange(value = "WEDO.BOOK.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
            key = {"book.delete"}
    ))
    public void delete(Long id) throws IOException {
        if (id == null) {
            return;
        }
        this.searchService.delete(id);
    }



}
